package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.TripDAO;
import com.zerosky.crm.po.Trip;
import com.zerosky.crm.service.TripService;
import com.zerosky.crm.util.PageInfo;

/**
 * Trip业务逻辑实现类
 * 
 * @author 钟璟;
 * @since 2017-07-13
 *
 */
@Service(value="tripService")
public class TripServiceImpl implements TripService {
	@Autowired
	private TripDAO TripDAO; // Trip数据接口

	/**
	 * Trip列表
	 * 
	 * @author 钟璟;
	 * @since 2017-07-13
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Trip> getTripList(Map map, PageInfo<Trip> page) {
		int begin = ((page.getPageNo() - 1) * page.getPageSize());
		int pageSize = page.getPageSize();

		// 写入数据
		map.put("pageNo", begin);
		map.put("pageSize", pageSize);
		List<Trip> list = TripDAO.getTripList(map);
		page.setList(list);

		int count = TripDAO.countTrip(map);// 获取总记录
		page.setPageCount(count);
		return page;
	}

	/**
	 * 通过主键查找Trip实体
	 * 
	 * @param id
	 *            Trip主键
	 * @author 钟璟;
	 * @since 2017-07-13
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Trip getTripById(String id) {
		return TripDAO.getTripById(id);
	}

	/**
	 * 保存Trip
	 * 
	 * @param Trip
	 *            实体对象, operation 操作类型
	 * @author 钟璟;
	 * @since 2017-07-13
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void saveTrip(Trip Trip, String operation) throws Exception {
		if ("新增".equalsIgnoreCase(operation)) {
			TripDAO.addTrip(Trip);
		} else if ("修改".equalsIgnoreCase(operation)) {
			TripDAO.updateTrip(Trip);
		}
	}

	/**
	 * 删除Trip
	 * 
	 * @param idArray
	 *            Trip主键数组
	 * @author 钟璟;
	 * @since 2017-07-13
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void deleteTripById(String id) throws Exception {
		TripDAO.deleteTripById(id);
	}

	/**
	 * 获取今日行程
	 * 
	 * @param idArray
	 *            Trip主键数组
	 * @author 钟璟;
	 * @since 2017-07-13
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Trip> getTodayTripList(Map<String, Object> map, PageInfo<Trip> page) {
		// 写入数据
		List<Trip> list = TripDAO.getTripList(map);
		page.setList(list);
		return page;
	}
}
